package com.sprucetec.live.common.dto;

import java.io.Serializable;
import java.util.List;

import com.sprucetec.live.common.exceptioin.ParamInvalidException;

public class PagedListDTO<T> implements Serializable {

	private static final long serialVersionUID = -6465604826576248342L;
	
	private List<T> records;
	private PageDTO page;
	private int total;

	public void checkInvalid() throws ParamInvalidException {
		if (this.total < 0)
			throw new ParamInvalidException("total", "总记录数必须大于或等于0");
		this.page.checkInvalid();
	}

	public int getTotalPage() {
		return this.total / this.page.getPageSize() + (this.total % this.page.getPageSize() > 0 ? 1 : 0);
	}

	public int getLastPageNo() {
		int lastPage = 0;
		if (this.total % this.page.getPageSize() == 0)
			lastPage = this.total / this.page.getPageSize();
		else {
			lastPage = this.total / this.page.getPageSize() + 1;
		}
		return lastPage;
	}

	public List<T> getRecords() {
		return this.records;
	}

	public PageDTO getPage() {
		return this.page;
	}

	public int getTotal() {
		return this.total;
	}

	public void setRecords(List<T> records) {		    
		this.records = records; 
	}
	
	public void setPage(PageDTO page) {
		this.page = page; 
	}
	
	public void setTotal(int total) {
		this.total = total; 
	} 
	
	public String toString() { 
		return "PagedListDTO(records=" + getRecords() + ", page=" + getPage() + ", total=" + getTotal() + ")";
	}
}
